package com.citywithincity.api.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.citywithincity.api.ApiFilter;
import com.damai.action.IAction;
import com.damai.action.IActionFactory;
import com.damai.action.IApiAction;
import com.damai.action.factories.ApiJsonActionFactory;
import com.damai.action.factories.MchJsonActionFactory;
import com.damai.action.factories.SimpleJsonApiFactory;
import com.damai.action.factories.WebJsonActionFactory;
import com.damai.action.meta.ApiMetaData;

public class ApiUtil {
	
	
	/**
	 * 需要签名和加密的接口
	 */
	public static final int API_TYPE_DEFAULT_JSON = 0; 

	/**
	 * 简单json(输入输出都是json)
	 */
	public static final int API_TYPE_SIMPLE_JSON = 1; 
	
	/**
	 * 采用querystring的方式传递参数，返回json
	 */
	public static final int API_TYPE_QUERYSTRING_JSON = 2; 
	
	/**
	 * 带商户的
	 */
	public static final int API_TYPE_MCH_JSON = 3; 
	
	
	
	
	public static int getApiType(Class<? extends IActionFactory> factoryClass) {
		if(factoryClass == ApiJsonActionFactory.class){
			return ApiUtil.API_TYPE_DEFAULT_JSON;
		}else if(factoryClass == MchJsonActionFactory.class){
			return ApiUtil.API_TYPE_MCH_JSON;
		}else if(factoryClass == SimpleJsonApiFactory.class){
			return ApiUtil.API_TYPE_SIMPLE_JSON;
		}else if(factoryClass == WebJsonActionFactory.class){
			return ApiUtil.API_TYPE_QUERYSTRING_JSON;
		}
		return 0;
	}
	
	public static final String deviceID = "deviceID";
	public static final String rand_str = "rand_str";
	public static final String time = "time";
	public static final String sign="sign";
	public static final String bsId = "bs_id";
	
	
	public static final String fileReader = "reader";
	

	private static List<ApiMetaData> usingApiList;
	
	public static List<ApiMetaData> getUsingApi(){
		if(usingApiList == null){
			synchronized (ApiUtil.class) {
				if(usingApiList == null){
					Map<String, IAction> map =new TreeMap<String, IAction>( ApiFilter.me().getActionMap());
					List<ApiMetaData> list = new ArrayList<ApiMetaData>(map.size());
					
					for (Entry<String, IAction> entry : map.entrySet()) {
						IAction action = entry.getValue();
						String key = action.getKey();
						if(key.startsWith("/structs")){
							continue;
						}
						if(key.startsWith("/system")){
							continue;
						}
						if(key.startsWith("/proxy_api")){
							continue;
						}
						if(key.startsWith("/jobCenter")){
							continue;
						}
						if(key.startsWith("/php")){
							continue;
						}
						if(action instanceof IApiAction){
							ApiMetaData data = ((IApiAction)action).getMetaData();
							list.add(data);
						}	
					}
					usingApiList = list;
				}
			}
		}
		
		
		return usingApiList;
	}
	
	
}
